package dao;

import model.AlergyDetail;
import model.User;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: TM
 * Date: 10/8/12
 * Time: 5:55 PM
 * To change this template use FileModel | Settings | FileModel Templates.
 */
public class UserDaoImpl extends JdbcDaoSupport implements UserDao {

    //add the customer
    public void addUser(User user) {
        // getJdbcTemplate().save(user);
    }

    public void updateUser(User user) {
        // getHibernateTemplate().saveOrUpdate(user);
    }

    public void registerUser(User user) {
        // getHibernateTemplate().save(user);
    }

    /* public List<User> listUser() {
        List<User> users = (List<User>) getHibernateTemplate().find("from User");
        return users;
    }*/

    public List<User> loginUser(final User user) {
        try {
            String query = "SELECT \n" +
                    "NIC, TITLE, FIRST_NAME, \n" +
                    "   LAST_NAME, ADDRESS, DOB, \n" +
                    "   PARENT_NIC, CONTACT_NO, TXN_DATE, \n" +
                    "   STATUS, PASSWORD, PIN_NO, \n" +
                    "   EMAIL\n" +
                    "FROM MEMBER M WHERE M.NIC = ? AND M.PASSWORD = ?";
            System.out.println(query);
            return (List<User>) getJdbcTemplate().query(query, new Object[]{user.getName(), user.getPassword()},
                    new RowMapper() {
                        public Object mapRow(ResultSet resultSet, int rowNum) throws SQLException {
                            System.out.println("User Id " + resultSet.getString("NIC"));
                            //User user = new User();
                            user.setNic(resultSet.getString("NIC"));
                            user.setFname(resultSet.getString("FIRST_NAME"));
                            user.setLname(resultSet.getString("LAST_NAME"));
                            user.setAddress(resultSet.getString("ADDRESS"));
                            user.setDob(resultSet.getDate("DOB"));
                            user.setContactNo(resultSet.getLong("CONTACT_NO"));
                            user.setName(resultSet.getString("LAST_NAME"));
                            user.setEmail(resultSet.getString("EMAIL"));
                            //user.setGender(resultSet.getString("GENDER"));
                            return user;
                        }

                    });
        } catch (EmptyResultDataAccessException e) {
            e.printStackTrace();
            return null;
        }
    }

    /*public List<User> recoverPassword(String name, String email) {
        return getHibernateTemplate().find("from User u where u.name='" + name + "' and u.email='" + email + "'");
    }*/

    public int updateUserInformation(User user) {
        try {
            String query = "UPDATE MEMBER\n" +
                    "SET    \n" +
                    "       TITLE      = ?,\n" +
                    "       FIRST_NAME = ?,\n" +
                    "       LAST_NAME  = ?,\n" +
                    "       ADDRESS    = ?,\n" +
                    "       DOB        = ?,\n" +
                    "       CONTACT_NO = ?,\n" +
                    "       TXN_DATE   = sysdate,\n" +
                    "       STATUS     = 1,\n" +
                    "       PASSWORD   = ?,\n" +
                    "       PIN_NO     = ?,\n" +
                    "       EMAIL      = ?\n" +
                    "WHERE  NIC        = ?";
            System.out.println(user.getDob() + query);
            return getJdbcTemplate().update(query, new Object[]{user.getTitle(), user.getFname(), user.getLname(), user.getAddress(), user.getDob()
                    , user.getContactNo(), user.getPassword(), user.getPinNo(), user.getEmail(), user.getNic()});
        } catch (EmptyResultDataAccessException e) {
            e.printStackTrace();
            return 0;
        } catch (Exception e) {
            System.out.println(e.toString());
            return 0;
        }
    }

    public List<AlergyDetail> getAlergyDetails(final User user){
              try {
            String query = "SELECT \n" +
                    "DRUG_NAME, DESCRIPTION\n" +
                    "FROM ALERGY_DETAIL WHERE NIC = ?";
            System.out.println(query);
            return (List<AlergyDetail>) getJdbcTemplate().query(query, new Object[]{user.getNic()},
                    new RowMapper() {
                        public Object mapRow(ResultSet resultSet, int rowNum) throws SQLException {
                            //System.out.println("User Id " + resultSet.getString("NIC"));
                            return new AlergyDetail(resultSet.getString("DRUG_NAME"), resultSet.getString("DESCRIPTION"));
                        }

                    });
        } catch (EmptyResultDataAccessException e) {
            e.printStackTrace();
            return null;
        }
    }



    public int saveAlergyDetails(final User user, final AlergyDetail alergyDetail){
              try {
            String query = "INSERT INTO ALERGY_DETAIL (\n" +
                    "   NIC, DRUG_NAME, DESCRIPTION, \n" +
                    "   TXN_DATE, STATUS) \n" +
                    "VALUES ( ?,? ,? ,\n" +
                    "  sysdate  ,1 )";
            System.out.println(query);
            return getJdbcTemplate().update(query, new Object[]{user.getNic(), alergyDetail.getDrugName(),
            alergyDetail.getDescription()});
        } catch (EmptyResultDataAccessException e) {
            e.printStackTrace();
            return 0;
        }
    }

}
